package com.post799.soundapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.bappi.custom.activities.CustomActivity;

import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Tab2Activity1 extends CustomActivity {
	
	private static final SimpleDateFormat SIMPLE_DATE_FORMAT1 = new SimpleDateFormat("hh");
	private static final SimpleDateFormat SIMPLE_DATE_FORMAT2 = new SimpleDateFormat("mm");

	private Typeface font ;
	private TextView textViewCentre;
	private TextView textViewLeft;
	private TextView textViewRight;
	private RelativeLayout mainLayout;
	
	private SharedPreferences sp;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
			setContentView(R.layout.layout_tab_2_screen_1);
			sp = PreferenceManager.getDefaultSharedPreferences(getParent());
			font = Typeface.createFromAsset(getAssets(), "digital-7.ttf");
			mainLayout = (RelativeLayout)findViewById(R.id.main_layout);
			textViewCentre = (TextView)findViewById(R.id.text_view_centre);
			textViewLeft = (TextView)findViewById(R.id.text_view_left);
			textViewRight = (TextView)findViewById(R.id.text_view_right);
			textViewCentre.setTypeface(font);
			textViewLeft.setTypeface(font);
			textViewRight.setTypeface(font);
			
			new AsyncTask<String, String, String>() {
				private Calendar calendar = Calendar.getInstance();
				private boolean toggle;
				protected void onPreExecute() {
					try {
						calendar.setTimeInMillis(System.currentTimeMillis());				
						textViewLeft.setText(SIMPLE_DATE_FORMAT1.format(calendar.getTime()));
						textViewRight.setText(SIMPLE_DATE_FORMAT2.format(calendar.getTime()));
						if(toggle){
							textViewCentre.setVisibility(View.VISIBLE);
						}else{
							textViewCentre.setVisibility(View.INVISIBLE);
						}
					
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				protected void onProgressUpdate(String... arg0) {
					try {
						calendar.setTimeInMillis(System.currentTimeMillis());				
						textViewLeft.setText(SIMPLE_DATE_FORMAT1.format(calendar.getTime()));
						textViewRight.setText(SIMPLE_DATE_FORMAT2.format(calendar.getTime()));
						if(toggle){
							textViewCentre.setVisibility(View.VISIBLE);
						}else{
							textViewCentre.setVisibility(View.INVISIBLE);
						}
					
					} catch (Exception e) {
						e.printStackTrace();
					}
				};

				protected String doInBackground(String... arg0) {
					try {
						while (!isFinishing()) {
							Thread.sleep(1000);
							toggle = (!toggle);
							publishProgress();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {
					

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();
			
			mainLayout.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					TabsController.toggleTabBar(sp);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		try {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
			TabsController.hideTabBar(sp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
